<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Publish/Subscribe and subscriptions to topics</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="sam.operations.html">Messaging operations</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="sam.errors.html">Error handling</a></div>
 <div class="up"><a href="sam.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="sam.pubsub" class="section">
  <h2 class="title">Publish/Subscribe and subscriptions to topics</h2>
  <p class="para">
   SAM allows messages to be sent either to queues or, for WebSphere MQ
   and WPM, to publish/subscribe topics.
   A topic destination is specified to SAM in the usual way, i.e. in the
   form &#039;topic://fred&#039;, rather than the form &#039;queue://AQUEUE&#039; used for
   point to point operation. To use publish/subscribe it is simply
   necessary to specify the correct broker name on the SAMConnect
   &quot;connect&quot; call and the desired topic in the destination argument to
   the SAMConnect &quot;send&quot; and &quot;receive&quot; calls. The PHP interface is
   otherwise identical to the point to point model.
  </p>
  <p class="para">
   By default, SAM creates non-durable subscriptions when using
   publish/subscribe. This means that if a client application is
   inactive when messages are published to a topic, then it will not
   receive them when it subsequently restarted. SAM does also allow
   durable subscriptions to be made to topics when using WPM or WebSphere
   MQ publish/subscribe. The purpose of these subscriptions is to allow
   data to be received by a client application even if that client was not
   active at the time the data was published.
  </p>
  <p class="para">
   Durable subscriptions are specified by using the SAMConnect &quot;subscribe&quot;
   call. This method takes the destination topic as an input parameter and
   returns a subscription identifier that may be used on subsequent
   &quot;receive&quot; calls. When the subscription is no longer required the
   SAMConnection &quot;unsubscribe&quot; method should be used to delete the
   subscription.
  </p>
  <p class="para">
   <div class="example" id="example-4417">
    <p><strong>Example #1 Creating a durable subscription to a topic</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$subName&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$conn</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">subscribe</span><span style="color: #007700">(</span><span style="color: #DD0000">'topic://A'</span><span style="color: #007700">);<br /><br />if&nbsp;(!</span><span style="color: #0000BB">$subName</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Subscribe&nbsp;failed"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;Subscribe&nbsp;was&nbsp;OK<br />&nbsp;&nbsp;&nbsp;//&nbsp;...<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4418">
    <p><strong>Example #2 Subscribing to a topic using a WebSphere Platform Messaging (WPM) server</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$conn&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SAMConnection</span><span style="color: #007700">();<br /></span><span style="color: #FF8000">//&nbsp;Note:&nbsp;For&nbsp;pub/sub&nbsp;on&nbsp;WPM,&nbsp;when&nbsp;connecting&nbsp;the&nbsp;name&nbsp;of&nbsp;a&nbsp;messaging&nbsp;engine<br />//&nbsp;&nbsp;&nbsp;to&nbsp;hold&nbsp;the&nbsp;durable&nbsp;subscription&nbsp;(SAM_WPM_DUR_SUB_HOME)&nbsp;must&nbsp;be&nbsp;specified.<br /></span><span style="color: #0000BB">$conn</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">connect</span><span style="color: #007700">(</span><span style="color: #0000BB">SAM_WMQ</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #0000BB">SAM_ENDPOINTS&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'localhost:7278:BootstrapBasicMessaging'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">SAM_BUS&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Bus1'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">SAM_TARGETCHAIN&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'InboundBasicMessaging'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">SAM_WPM_DUR_SUB_HOME&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'MyMachineNode01.server1-Bus1'</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">$subName&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$conn</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">subscribe</span><span style="color: #007700">(</span><span style="color: #DD0000">'topic://A'</span><span style="color: #007700">);<br /><br />if&nbsp;(!</span><span style="color: #0000BB">$subName</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Subscribe&nbsp;failed"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;Subscribe&nbsp;was&nbsp;OK<br />&nbsp;&nbsp;&nbsp;//&nbsp;...<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4419">
    <p><strong>Example #3 Receiving published data using a durable subscription</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$msg&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$conn</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">receive</span><span style="color: #007700">(</span><span style="color: #0000BB">$subName</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">$msg</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Received&nbsp;a&nbsp;message&nbsp;OK"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"The&nbsp;receive&nbsp;failed"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4420">
    <p><strong>Example #4 Deleting a durable subscription to a topic</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">if&nbsp;(!</span><span style="color: #0000BB">$conn</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">unsubscribe</span><span style="color: #007700">(</span><span style="color: #0000BB">$subName</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Unsubscribe&nbsp;failed"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="sam.operations.html">Messaging operations</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="sam.errors.html">Error handling</a></div>
 <div class="up"><a href="sam.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
